home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / VMRXcl / d3dutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  10.7 KB  |  303 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DUtil.cpp
  3. //
  4. // Desc: Shortcut macros and functions for using DX objects
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved
  7. //-----------------------------------------------------------------------------
  8. #include <streams.h>
  9.  
  10. #define D3D_OVERLOADS
  11. #include <math.h>
  12. #include "D3DUtil.h"
  13.  
  14. #ifndef _T
  15. #define _T TEXT
  16. #endif
  17.  
  18. //-----------------------------------------------------------------------------
  19. // Name: D3DUtil_GetDXSDKMediaPath()
  20. // Desc: Returns the DirectX SDK media path, as stored in the system registry
  21. //       during the SDK install.
  22. //-----------------------------------------------------------------------------
  23. const TCHAR* D3DUtil_GetDXSDKMediaPath()
  24. {
  25.     static TCHAR strNull[2] = _T("");
  26.     static TCHAR strPath[512];
  27.     HKEY  key;
  28.     DWORD type, size = 512;
  29.  
  30.     // Open the appropriate registry key
  31.     LONG result = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  32.                                 _T("Software\\Microsoft\\DirectX"),
  33.                                 0, KEY_READ, &key );
  34.     if( ERROR_SUCCESS != result )
  35.         return strNull;
  36.  
  37.     result = RegQueryValueEx( key, _T("DX81SDK Samples Path"), NULL,
  38.                               &type, (BYTE*)strPath, &size );
  39.     RegCloseKey( key );
  40.  
  41.     if( ERROR_SUCCESS != result )
  42.     {
  43.         result = RegQueryValueEx( key, _T("DX8SDK Samples Path"), NULL,
  44.                                   &type, (BYTE*)strPath, &size );
  45.         RegCloseKey( key );
  46.  
  47.         if( ERROR_SUCCESS != result )
  48.         {
  49.             result = RegQueryValueEx( key, _T("DXSDK Samples Path"), NULL,
  50.                                       &type, (BYTE*)strPath, &size );
  51.             RegCloseKey( key );
  52.  
  53.             if( ERROR_SUCCESS != result )
  54.                 return strNull;
  55.         }
  56.     }
  57.  
  58.     lstrcat( strPath, _T("\\D3DIM\\Media\\") );
  59.  
  60.     return strPath;
  61. }
  62.  
  63.  
  64. //-----------------------------------------------------------------------------
  65. // Name: D3DUtil_InitSurfaceDesc()
  66. // Desc: Helper function called to build a DDSURFACEDESC2 structure,
  67. //       typically before calling CreateSurface() or GetSurfaceDesc()
  68. //-----------------------------------------------------------------------------
  69. VOID D3DUtil_InitSurfaceDesc( DDSURFACEDESC2& ddsd, DWORD dwFlags,
  70.                               DWORD dwCaps )
  71. {
  72.     ZeroMemory( &ddsd, sizeof(ddsd) );
  73.     ddsd.dwSize                 = sizeof(ddsd);
  74.     ddsd.dwFlags                = dwFlags;
  75.     ddsd.ddsCaps.dwCaps         = dwCaps;
  76.     ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  77. }
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Name: D3DUtil_InitMaterial()
  82. // Desc: Helper function called to build a D3DMATERIAL7 structure
  83. //-----------------------------------------------------------------------------
  84. VOID D3DUtil_InitMaterial( D3DMATERIAL7& mtrl, FLOAT r, FLOAT g, FLOAT b,
  85.                            FLOAT a )
  86. {
  87.     ZeroMemory( &mtrl, sizeof(D3DMATERIAL7) );
  88.     mtrl.dcvDiffuse.r = mtrl.dcvAmbient.r = r;
  89.     mtrl.dcvDiffuse.g = mtrl.dcvAmbient.g = g;
  90.     mtrl.dcvDiffuse.b = mtrl.dcvAmbient.b = b;
  91.     mtrl.dcvDiffuse.a = mtrl.dcvAmbient.a = a;
  92. }
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Name: D3DUtil_InitLight()
  97. // Desc: Initializes a D3DLIGHT7 structure
  98. //-----------------------------------------------------------------------------
  99. VOID D3DUtil_InitLight( D3DLIGHT7& light, D3DLIGHTTYPE ltType,
  100.                         FLOAT x, FLOAT y, FLOAT z )
  101. {
  102.     ZeroMemory( &light, sizeof(D3DLIGHT7) );
  103.     light.dltType        = ltType;
  104.     light.dcvDiffuse.r   = 1.0f;
  105.     light.dcvDiffuse.g   = 1.0f;
  106.     light.dcvDiffuse.b   = 1.0f;
  107.     light.dcvSpecular    = light.dcvDiffuse;
  108.     light.dvPosition.x   = light.dvDirection.x = x;
  109.     light.dvPosition.y   = light.dvDirection.y = y;
  110.     light.dvPosition.z   = light.dvDirection.z = z;
  111.     light.dvAttenuation0 = 1.0f;
  112.     light.dvRange        = D3DLIGHT_RANGE_MAX;
  113. }
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Name: D3DUtil_SetViewMatrix()
  118. // Desc: Given an eye point, a lookat point, and an up vector, this
  119. //       function builds a 4x4 view matrix.
  120. //-----------------------------------------------------------------------------
  121. HRESULT D3DUtil_SetViewMatrix( D3DMATRIX& mat, D3DVECTOR& vFrom,
  122.                                D3DVECTOR& vAt, D3DVECTOR& vWorldUp )
  123. {
  124.     // Get the z basis vector, which points straight ahead. This is the
  125.     // difference from the eyepoint to the lookat point.
  126.     D3DVECTOR vView = vAt - vFrom;
  127.  
  128.     FLOAT fLength = Magnitude( vView );
  129.     if( fLength < 1e-6f )
  130.         return E_INVALIDARG;
  131.  
  132.     // Normalize the z basis vector
  133.     vView /= fLength;
  134.  
  135.     // Get the dot product, and calculate the projection of the z basis
  136.     // vector onto the up vector. The projection is the y basis vector.
  137.     FLOAT fDotProduct = DotProduct( vWorldUp, vView );
  138.  
  139.     D3DVECTOR vUp = vWorldUp - fDotProduct * vView;
  140.  
  141.     // If this vector has near-zero length because the input specified a
  142.     // bogus up vector, let's try a default up vector
  143.     if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  144.     {
  145.         vUp = D3DVECTOR( 0.0f, 1.0f, 0.0f ) - vView.y * vView;
  146.  
  147.         // If we still have near-zero length, resort to a different axis.
  148.         if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  149.         {
  150.             vUp = D3DVECTOR( 0.0f, 0.0f, 1.0f ) - vView.z * vView;
  151.  
  152.             if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
  153.                 return E_INVALIDARG;
  154.         }
  155.     }
  156.  
  157.     // Normalize the y basis vector
  158.     vUp /= fLength;
  159.  
  160.     // The x basis vector is found simply with the cross product of the y
  161.     // and z basis vectors
  162.     D3DVECTOR vRight = CrossProduct( vUp, vView );
  163.  
  164.     // Start building the matrix. The first three rows contains the basis
  165.     // vectors used to rotate the view to point at the lookat point
  166.     D3DUtil_SetIdentityMatrix( mat );
  167.     mat._11 = vRight.x;    mat._12 = vUp.x;    mat._13 = vView.x;
  168.     mat._21 = vRight.y;    mat._22 = vUp.y;    mat._23 = vView.y;
  169.     mat._31 = vRight.z;    mat._32 = vUp.z;    mat._33 = vView.z;
  170.  
  171.     // Do the translation values (rotations are still about the eyepoint)
  172.     mat._41 = - DotProduct( vFrom, vRight );
  173.     mat._42 = - DotProduct( vFrom, vUp );
  174.     mat._43 = - DotProduct( vFrom, vView );
  175.  
  176.     return S_OK;
  177. }
  178.  
  179.  
  180. //-----------------------------------------------------------------------------
  181. // Name: D3DUtil_SetProjectionMatrix()
  182. // Desc: Sets the passed in 4x4 matrix to a perpsective projection matrix built
  183. //       from the field-of-view (fov, in y), aspect ratio, near plane (D),
  184. //       and far plane (F). Note that the projection matrix is normalized for
  185. //       element [3][4] to be 1.0. This is performed so that W-based range fog
  186. //       will work correctly.
  187. //-----------------------------------------------------------------------------
  188. HRESULT D3DUtil_SetProjectionMatrix( D3DMATRIX& mat, FLOAT fFOV, FLOAT fAspect,
  189.                                      FLOAT fNearPlane, FLOAT fFarPlane )
  190. {
  191.     if( fabs(fFarPlane-fNearPlane) < 0.01f )
  192.         return E_INVALIDARG;
  193.     if( fabs(sin(fFOV/2)) < 0.01f )
  194.         return E_INVALIDARG;
  195.  
  196.     FLOAT w = fAspect * ( cosf(fFOV/2)/sinf(fFOV/2) );
  197.     FLOAT h =   1.0f  * ( cosf(fFOV/2)/sinf(fFOV/2) );
  198.     FLOAT Q = fFarPlane / ( fFarPlane - fNearPlane );
  199.  
  200.     ZeroMemory( &mat, sizeof(D3DMATRIX) );
  201.     mat._11 = w;
  202.     mat._22 = h;
  203.     mat._33 = Q;
  204.     mat._34 = 1.0f;
  205.     mat._43 = -Q*fNearPlane;
  206.  
  207.     return S_OK;
  208. }
  209.  
  210.  
  211. //-----------------------------------------------------------------------------
  212. // Name: D3DUtil_SetRotateXMatrix()
  213. // Desc: Create Rotation matrix about X axis
  214. //-----------------------------------------------------------------------------
  215. VOID D3DUtil_SetRotateXMatrix( D3DMATRIX& mat, FLOAT fRads )
  216. {
  217.     D3DUtil_SetIdentityMatrix( mat );
  218.     mat._22 =  cosf( fRads );
  219.     mat._23 =  sinf( fRads );
  220.     mat._32 = -sinf( fRads );
  221.     mat._33 =  cosf( fRads );
  222. }
  223.  
  224.  
  225. //-----------------------------------------------------------------------------
  226. // Name: D3DUtil_SetRotateYMatrix()
  227. // Desc: Create Rotation matrix about Y axis
  228. //-----------------------------------------------------------------------------
  229. VOID D3DUtil_SetRotateYMatrix( D3DMATRIX& mat, FLOAT fRads )
  230. {
  231.     D3DUtil_SetIdentityMatrix( mat );
  232.     mat._11 =  cosf( fRads );
  233.     mat._13 = -sinf( fRads );
  234.     mat._31 =  sinf( fRads );
  235.     mat._33 =  cosf( fRads );
  236. }
  237.  
  238.  
  239. //-----------------------------------------------------------------------------
  240. // Name: D3DUtil_SetRotateZMatrix()
  241. // Desc: Create Rotation matrix about Z axis
  242. //-----------------------------------------------------------------------------
  243. VOID D3DUtil_SetRotateZMatrix( D3DMATRIX& mat, FLOAT fRads )
  244. {
  245.     D3DUtil_SetIdentityMatrix( mat );
  246.     mat._11  =  cosf( fRads );
  247.     mat._12  =  sinf( fRads );
  248.     mat._21  = -sinf( fRads );
  249.     mat._22  =  cosf( fRads );
  250. }
  251.  
  252.  
  253. //-----------------------------------------------------------------------------
  254. // Name: D3DUtil_SetRotationMatrix
  255. // Desc: Create a Rotation matrix about vector direction
  256. //-----------------------------------------------------------------------------
  257. VOID D3DUtil_SetRotationMatrix( D3DMATRIX& mat, D3DVECTOR& vDir, FLOAT fRads )
  258. {
  259.     FLOAT     fCos = cosf( fRads );
  260.     FLOAT     fSin = sinf( fRads );
  261.     D3DVECTOR v    = Normalize( vDir );
  262.  
  263.     mat._11 = ( v.x * v.x ) * ( 1.0f - fCos ) + fCos;
  264.     mat._12 = ( v.x * v.y ) * ( 1.0f - fCos ) - (v.z * fSin);
  265.     mat._13 = ( v.x * v.z ) * ( 1.0f - fCos ) + (v.y * fSin);
  266.  
  267.     mat._21 = ( v.y * v.x ) * ( 1.0f - fCos ) + (v.z * fSin);
  268.     mat._22 = ( v.y * v.y ) * ( 1.0f - fCos ) + fCos ;
  269.     mat._23 = ( v.y * v.z ) * ( 1.0f - fCos ) - (v.x * fSin);
  270.  
  271.     mat._31 = ( v.z * v.x ) * ( 1.0f - fCos ) - (v.y * fSin);
  272.     mat._32 = ( v.z * v.y ) * ( 1.0f - fCos ) + (v.x * fSin);
  273.     mat._33 = ( v.z * v.z ) * ( 1.0f - fCos ) + fCos;
  274.  
  275.     mat._14 = mat._24 = mat._34 = 0.0f;
  276.     mat._41 = mat._42 = mat._43 = 0.0f;
  277.     mat._44 = 1.0f;
  278. }
  279.  
  280.  
  281. //-----------------------------------------------------------------------------
  282. // Name: _DbgOut()
  283. // Desc: Outputs a message to the debug stream
  284. //-----------------------------------------------------------------------------
  285. HRESULT _DbgOut( CHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
  286. {
  287.     TCHAR buffer[256];
  288.     wsprintf( buffer, _T("%hs(%ld): "), strFile, dwLine );
  289.     OutputDebugString( buffer );
  290.     OutputDebugString( strMsg );
  291.  
  292.     if( hr )
  293.     {
  294.         wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
  295.         OutputDebugString( buffer );
  296.     }
  297.  
  298.     OutputDebugString( _T("\n") );
  299.  
  300.     return hr;
  301. }
  302.  
  303.